package com.aispeech.jarpc.op.ssc;

import com.aispeech.jarpc.TextBin;
import com.aispeech.jarpc.op.AbstractRequest;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class SscRequest extends AbstractRequest {
    private final static String REQ_TMPL = "{\"op\":\"ssc\",\"sample_rate\":%d,\"pcm_fpath\":\"%s\",\"recluster_fpath\":\"%s\",\"recluster_fpath_purpose\":%d,\"speakers\":%d,\"threads\":%d,\"total\":%d,\"feed\":%s}";

    private int sampleRate;
    private String pcmPath;
    private int threads;
    private int speakers;
    private int total;
    private String feed;
    private String sscTmpFilePath;

    public SscRequest(int sampleRate, String pcmPath, int total, int threads, int speakers) {
        this(sampleRate, pcmPath, "", total, threads, speakers);
    }

    public SscRequest(int sampleRate, String pcmPath, String sscTmpFilePath, int total, int threads, int speakers) {
        this.sampleRate = sampleRate;
        this.pcmPath = pcmPath;
        this.total = total;
        this.speakers = speakers;
        this.threads = threads;
        this.sscTmpFilePath = sscTmpFilePath;

        this.feed = "null";
    }

    public TextBin toJsonBin() {
        return new TextBin(toJson(), null);
    }

    @Override
    public String toJson() {
        int ressc = StringUtils.isNotEmpty(sscTmpFilePath) && new File(sscTmpFilePath).exists() ? 1 : 0;
        return String.format(REQ_TMPL, sampleRate, pcmPath, sscTmpFilePath, ressc, speakers, threads, total, feed);
    }

    @Override
    public String OpType() {
        return "ssc";
    }

    public void setFeed(String feed_string) {
        this.feed = feed_string;
    }
}
